﻿using System;
using System.ComponentModel;
using Needle.Framework;

namespace Needle.Core
{
    /// <summary>
    /// 
    /// </summary>
    public class NeedleContext : LifetimeComponent, IContext
    {
        private Needle.Framework.NestedContainer container;

        /// <summary>
        /// NeedleContext
        /// </summary>
        public NeedleContext()
        {
            this.container = new Needle.Framework.NestedContainer(this, true);
        }
        /// <summary>
        /// NeedleContext
        /// </summary>
        public NeedleContext(IContainer container)
        {
            container.Add(this);
            this.container = new Needle.Framework.NestedContainer(this, true);
        }

        #region IServiceProvider Members

        /// <summary>
        /// returns the requested service or null
        /// </summary>
        object IServiceProvider.GetService(Type serviceType)
        {
            return GetService(serviceType);
        }

        #endregion

        /// <summary>
        /// returns the requested service or null
        /// </summary>
        protected override object GetService(Type serviceType)
        {
            if (typeof(IContext) == serviceType)
                return this;
            object service = ((IServiceProvider)container).GetService(serviceType);
            if (service == null)
                service = base.GetService(serviceType);
            return service;
        }

        #region IContext Members

        /// <summary>
        /// the name of the context.
        /// </summary>
        public virtual string Name
        {
            get
            {
                ISite site = Site;
                if (site != null)
                    return site.Name;
                return null;
            }
        }
        /// <summary>
        /// The full name of the context.
        /// </summary>
        public virtual string FullName
        {
            get
            {
                INestedSite site = Site as INestedSite;
                if (site != null)
                    return site.FullName;
                return Name;
            }
        }

        #endregion

        #region Container methods

        /// <summary>
        /// Adds a component with a name
        /// </summary>
        protected virtual void Add(IComponent component, string name)
        {
            container.Add(component, name);
        }

        /// <summary>
        /// Adds a component without a name
        /// </summary>
        protected virtual void Add(IComponent component)
        {
            container.Add(component);
        }
        /// <summary>
        /// The list of components
        /// </summary>
        protected ComponentCollection Components
        {
            get { return container.Components; }
        }
        /// <summary>
        /// Remove a component.
        /// </summary>
        protected virtual void Remove(IComponent component)
        {
            container.Remove(component);
        }

        #endregion
        /// <summary></summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                container.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
